package com.zy.smartdb.util;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: zara
 * @Version V3.0
 * @Date: 2022/9/29 3:19 下午
 */

public class StringUtil {

    public static String fillSpace(String str) {
        if (str.contains(" ")) {
            return str.replaceAll("\\s", "");
        }
        return str;
    }

    public static Object safetyClone(Object obj) throws Exception {
        if (obj instanceof String) {
            return fillSpace((String) obj);
        } else if (obj==null||obj instanceof Enum<?>||obj.getClass().getName().startsWith("java.lang")) {
            return obj;
        } else if (obj instanceof Collection) {
            Collection collection = (Collection) obj.getClass().newInstance();
            for (Object ob : (Collection) obj) {
                collection.add(safetyClone(ob));
            }
            return collection;
        } else if (obj instanceof Map) {
//todo
            return obj;
        } else {
            return clone(obj);
        }

    }

    public static Object clone(Object obj) throws Exception {
        Object obj2 = obj.getClass().newInstance();
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field f1 : fields) {
            Field f2 = obj2.getClass().getDeclaredField(f1.getName());
            f1.setAccessible(true);
            f2.setAccessible(true);
            Object val1 = f1.get(obj);
            f2.set(obj2, safetyClone(val1));
        }
        return obj2;
    }


    public static boolean isNullOrEmpty(Object id) {
        if (id != null) {
            String idString = id.toString();
            return idString.isEmpty();
        }
        return true;
    }


//    public static Collection safetyCloneCllection(Collection collection) throws NoSuchFieldException, InstantiationException, IllegalAccessException {
//        Collection c = Collections.
//        for (Object obj : collection) {
//            Object newObj = null;
//            if (obj instanceof String || obj.getClass().getName().startsWith("com.znyw")) {
//                newObj = safetyClone(obj);
//            } else if (obj instanceof Collection) {
//                newObj = safetyCloneCllection((Collection) obj);
//            } else {
//                newObj = obj.getClass().get
//            }
//            c.add(newObj);
//            obj
//        }
//
//        collection.
//        return c;
//    }
}
